package com.qzsoft.zhjd.materiel.modules.materieldatasend.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qzsoft.jeemis.common.utils.Result;
import com.qzsoft.zhjd.common.utils.ResultUtils;
import com.qzsoft.zhjd.materiel.modules.materieldata.dto.MaterielDataCountDto;
import com.qzsoft.zhjd.materiel.modules.materieldata.dto.MaterielDataCountTypeDto;
import com.qzsoft.zhjd.materiel.modules.materieldata.dto.MaterielDataForCategoryChartDto;
import com.qzsoft.zhjd.materiel.modules.materieldata.dto.MaterielDataForItemChartDto;
import com.qzsoft.zhjd.materiel.modules.materieldata.dto.MaterielDataListDto;
import com.qzsoft.zhjd.materiel.modules.materieldata.enmus.WeightTypeEnum;
import com.qzsoft.zhjd.materiel.modules.materieldata.service.MaterielDataService;
import com.qzsoft.zhjd.materiel.modules.materieldatasend.dto.MaterielSendForCategoryDto;
import com.qzsoft.zhjd.materiel.modules.materieldatasend.dto.MaterielSendForItemDto;
import com.qzsoft.zhjd.materiel.modules.materieldatasend.service.MaterielDataSendService;

/**
 * @ClassName MaterielDataSendController
 * @Description 发料数据控制器
 * @author linhuawei
 * @since 2022-04-01
 */
@RestController
@RequestMapping("/api/materielDataSend")
public class MaterielDataSendController {

    @Autowired
    private MaterielDataService materielDataService;

    @Autowired
    private MaterielDataSendService materielDataSendService;

    /**
     * @Title count
     * @Description 发料台账统计信息
     * @param params 查询条件
     * @return 发料台账统计信息
     */
    @GetMapping("/count")
    public Result<MaterielDataCountDto> count(@RequestParam Map<String, Object> params) {
        params.put("weightType", WeightTypeEnum.SEND.getValue());
        MaterielDataCountDto count = materielDataService.countDto(params);
        return ResultUtils.ok(count);
    }
    
    /**
     * @Title countByWeightDataType
     * @Description 发料台账统计信息
     * @param params 查询条件
     * @return 发料台账统计信息
     */
    @GetMapping("/countByWeightDataType")
    public Result<MaterielDataCountTypeDto> countByWeightDataType(@RequestParam Map<String, Object> params) {
        params.put("weightType", WeightTypeEnum.SEND.getValue());
        MaterielDataCountTypeDto count = materielDataService.countByWeightDataType(params);
        return ResultUtils.ok(count);
    }

    /********************* 发料台账开始 *********************************/

    /**
     * @Title page
     * @Description 发料台账列表
     * @param params 查询条件
     * @return 发料台账列表
     */
    @RequiresPermissions("materiel:datasendbook")
    @GetMapping("/page")
    public Result<IPage<MaterielDataListDto>> page(@RequestParam Map<String, Object> params) {
        params.put("weightType", WeightTypeEnum.SEND.getValue());
        IPage<MaterielDataListDto> page = materielDataService.pageDto(params);
        return ResultUtils.ok(page);
    }

    /**
     * @Title exportForBook
     * @Description 台账导出
     * @param params 查询条件
     */
    @RequiresPermissions("materiel:datasendbook")
    @GetMapping("/exportBook")
    public void exportBook(@RequestParam Map<String, Object> params, HttpServletResponse response) {
        materielDataSendService.exportBook(params, response);
    }

    /********************* 发料台账结束 *********************************/

    /********************* 发料统计图表开始 *********************************/

    /**
     * @Title chartForCategory
     * @Description 按照材料类型统计
     * @param params 查询条件
     * @return 数据列表
     */
    @RequiresPermissions("materiel:datasendchart")
    @GetMapping("/chartForCategory")
    public Result<List<MaterielDataForCategoryChartDto>> chartForCategory(@RequestParam Map<String, Object> params) {
        params.put("weightType", WeightTypeEnum.SEND.getValue());
        List<MaterielDataForCategoryChartDto> list = materielDataService.countForCategoryChart(params);
        return ResultUtils.ok(list);
    }

    /**
     * @Title chartForItem
     * @Description 按照材料统计
     * @param params 查询条件
     * @return 数据列表
     */
    @RequiresPermissions("materiel:datasendchart")
    @GetMapping("/chartForItem")
    public Result<List<MaterielDataForItemChartDto>> chartForItem(@RequestParam Map<String, Object> params) {
        params.put("weightType", WeightTypeEnum.SEND.getValue());
        List<MaterielDataForItemChartDto> list = materielDataService.countForItemChart(params);
        return ResultUtils.ok(list);
    }

    /********************* 发料统计图表结束 *********************************/

    /********************* 发料统计报表开始 *********************************/

    /**
     * @Title pageForCategory
     * @Description 按照材料类型统计
     * @param params 查询条件
     * @return 数据列表
     */
    @RequiresPermissions("materiel:datasendreport")
    @GetMapping("/countForCategory")
    public Result<IPage<MaterielSendForCategoryDto>> countForCategory(@RequestParam Map<String, Object> params) {
        IPage<MaterielSendForCategoryDto> page = materielDataSendService.countForCategory(params);
        return ResultUtils.ok(page);
    }

    /**
     * @Title pageForItem
     * @Description 按照材料统计
     * @param params 查询条件
     * @return 数据列表
     */
    @RequiresPermissions("materiel:datasendreport")
    @GetMapping("/countForItem")
    public Result<IPage<MaterielSendForItemDto>> countForItem(@RequestParam Map<String, Object> params) {
        IPage<MaterielSendForItemDto> page = materielDataSendService.countForItem(params);
        return ResultUtils.ok(page);
    }

    /**
     * @Title exportCount
     * @Description 台账统计信息
     * @param params 查询条件
     */
    @RequiresPermissions("materiel:datasendreport")
    @GetMapping("/countExport")
    public void countExport(@RequestParam Map<String, Object> params, HttpServletResponse response) {
        materielDataSendService.countExport(params, response);
    }

    /********************* 发料统计报表结束 *********************************/

    /**
     * @Title getLastDay
     * @Description 获取最新一条数据日期
     * @return 最新一条数据日期
     */
    @GetMapping("/getLastDay")
    public Result<String> getLastDay() {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("weightType", WeightTypeEnum.SEND.getValue());
        String day = materielDataService.getLastDay(params);
        return ResultUtils.ok(day);
    }

    /**
     * @Title getLastMonth
     * @Description 获取最新一条数据月份
     * @return 最新一条数据月份
     */
    @GetMapping("/getLastMonth")
    public Result<String> getLastMonth() {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("weightType", WeightTypeEnum.SEND.getValue());
        String month = materielDataService.getLastMonth(params);
        return ResultUtils.ok(month);
    }

    /**
     * @Title getLastYear
     * @Description 获取最新一条数据年份
     * @return 最新一条数据年份
     */
    @GetMapping("/getLastYear")
    public Result<String> getLastYear() {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("weightType", WeightTypeEnum.SEND.getValue());
        String year = materielDataService.getLastYear(params);
        return ResultUtils.ok(year);
    }

}
